package com.cmc.cloud.cmclink.doc.controller.admin.si;

import cn.hutool.core.bean.BeanUtil;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.convert.BlnoCntrRecordationConvert;
import com.cmc.cloud.cmclink.doc.entity.BlnoCntrRecordationDO;
import com.cmc.cloud.cmclink.doc.service.si.BlnoCntrRecordationService;
import com.cmc.cloud.cmclink.doc.vo.blnocntrrecordationvo.*;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.operatelog.core.annotations.OperateLog;
import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;


/**
 * 提单箱况备案前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 提单箱况备案")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/cntrRecord")
public class BlnoCntrRecordationController {

    @Resource
    private BlnoCntrRecordationService blnoCntrRecordationService;

    @Operation(summary = "创建提单箱况备案")
    @OperateLog(type = OperateTypeEnum.CREATE)
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('doc:shipment:uploadContainerRecordation')")
    public CommonResult<Void> createBlnoCntrRecordation(@Valid @RequestBody BlnoCntrRecordationOperationVO createReqVO) {
        blnoCntrRecordationService.createBlnoCntrRecordation(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新提单箱况备案")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission('doc:blno-cntr-recordation:update')")
    public CommonResult<Void> updateBlnoCntrRecordation(@Valid @RequestBody BlnoCntrRecordationUpdateReqVO updateReqVO) {
        blnoCntrRecordationService.updateBlnoCntrRecordation(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除提单箱况备案")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('doc:blno-cntr-recordation:delete')")
    public CommonResult<Void> deleteBlnoCntrRecordation(@PathVariable("id") Long id) {
        blnoCntrRecordationService.deleteBlnoCntrRecordation(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得提单箱况备案")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/get/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('doc:blno-cntr-recordation:query')")
    public CommonResult<BlnoCntrRecordationRespVO> getBlnoCntrRecordation(@PathVariable("id") Long id) {
        BlnoCntrRecordationDO blnoCntrRecordation = blnoCntrRecordationService.getBlnoCntrRecordation(id);
        return CommonResult.success(BlnoCntrRecordationConvert.INSTANCE.convert(blnoCntrRecordation));
    }

    @Operation(summary = "获得提单箱况备案列表")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/list")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('doc:blno-cntr-recordation:query')")
    public CommonResult<List<BlnoCntrRecordationRespVO>> getBlnoCntrRecordationList(@RequestParam("ids") Collection<Long> ids) {
        List<BlnoCntrRecordationDO> list = blnoCntrRecordationService.getBlnoCntrRecordationList(ids);
        return CommonResult.success(BlnoCntrRecordationConvert.INSTANCE.convertList(list));
    }

    @Operation(summary = "获得提单箱况备案列表")
    @OperateLog(type = OperateTypeEnum.GET)
    @PostMapping("/recordList")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:shipment:viewContainerRecordation')")
    public CommonResult<List<BlnoCntrRecordationRespVO>> getCntrRecordationList(@Valid @RequestBody BlnoCntrRecordationQuery queryCntrRecord) {
        List<BlnoCntrRecordationDO> cntrRecordationList = blnoCntrRecordationService.getCntrRecordationList(queryCntrRecord);
        return CommonResult.success(BeanUtil.copyToList(cntrRecordationList, BlnoCntrRecordationRespVO.class));
    }

    @Operation(summary = "获得提单箱况备案分页")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('doc:blno-cntr-recordation:query')")
    public CommonResult<PageResult<BlnoCntrRecordationRespVO>> getBlnoCntrRecordationPage(@Valid @RequestBody BlnoCntrRecordationPageReqVO pageVO) {
        PageResult<BlnoCntrRecordationDO> pageResult = blnoCntrRecordationService.getBlnoCntrRecordationPage(pageVO);
        List<BlnoCntrRecordationRespVO> results = BlnoCntrRecordationConvert.INSTANCE.convertList(pageResult.getList());
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }
}
